/*
 * @Author: your name
 * @Date: 2021-06-21 20:45:31
 * @LastEditTime: 2021-06-22 13:02:40
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \scb-control-program\APP\STM32Communication.c
 */
#include "STM32Communication.h"
#include "usart.h"
uint8_t MCU_Receive[RECEIVE_SIZE] = {0};
chassis_receive_t Chassis_Receive;
chassis_board_u Chassis_Send;
void MCU_Receive_Data(uint8_t *pData,uint8_t datasize)
{
	uint8_t sum = 0;
 	if(pData[0]==0x5A)
	{
		sum = pData[0] + pData[1];
		if(sum == pData[2])
		{
			Chassis_Receive.Start = pData[0];
			Chassis_Send.Chassis_Board.Mode = Chassis_Receive.Mode = (pData[1] & 0x03);
			Chassis_Receive.Check = pData[1]>>2;
			Chassis_Receive.Default_bit = pData[1]>>3;
			Chassis_Receive.Sum_check =  pData[2];
		}
	}
}


void MCU_Transmit_Data()
{
	uint8_t transmit_control[3] = {0};
	uint8_t transmit_data_con[19] = {0};
	Chassis_Send.Chassis_Board.Start = 0X5A;
	if(Chassis_Send.Chassis_Board.Check == 1) //����ģʽ����+INA226���ݷ���
	{
		for(int i =0;i<18;i++)
		{
			Chassis_Send.Chassis_Board.Sum_check += Chassis_Send.send_data[i];
			transmit_data_con[i] =  Chassis_Send.send_data[i];
		}
		transmit_data_con[18] = Chassis_Send.Chassis_Board.Sum_check;
		HAL_UART_Transmit(&huart1,(uint8_t *)transmit_data_con,19,500);
		Chassis_Send.Chassis_Board.Sum_check = 0;
	}
	else if(Chassis_Send.Chassis_Board.Check == 0) //����ģʽ����
	{
		Chassis_Send.Chassis_Board.Sum_check = Chassis_Send.send_data[0] + Chassis_Send.send_data[1];
		Chassis_Send.send_data[2] = Chassis_Send.Chassis_Board.Sum_check ;
		transmit_control[0] = Chassis_Send.send_data[0];
		transmit_control[1] = Chassis_Send.send_data[1];
		transmit_control[2] = Chassis_Send.Chassis_Board.Sum_check ;
		HAL_UART_Transmit(&huart1,(uint8_t *)transmit_control,3,500);
	}
}
uint32_t data_length=0;
void BothMCU_Data_Process()
{
	    if(USART1 == huart1.Instance)                                   
			{	
        if(RESET != __HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE))   
        {	 
            __HAL_UART_CLEAR_IDLEFLAG(&huart1);   
            
            HAL_UART_DMAStop(&huart1);                                
          
            data_length  = RECEIVE_SIZE -__HAL_DMA_GET_COUNTER(&hdma_usart1_rx);

					  uint8_t pData[RECEIVE_SIZE]={0};
						memset(pData,0,sizeof(pData));
						memcpy(pData,MCU_Receive,data_length);
						if(data_length == 3)
							MCU_Receive_Data((uint8_t *)pData,data_length);
						MCU_Transmit_Data();
           
            HAL_UART_Receive_DMA(&huart1, (uint8_t*)MCU_Receive, RECEIVE_SIZE);   
					                       
        }
    }
}





